/*
 * $HeadURL:  $
 *
 * Copyright (c) 2011 Ericsson AB Corporation, all rights reserved.
 *
 */
package org.jample.spring.ws.restful;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author eccicce
 * @version $Revision: $
 */
@Controller
@RequestMapping(value = RestfulDemoEndpoint.BASE_PATH)
public class RestfulDemoEndpoint {

    public static final String BASE_PATH = "/1.0/**";

    /** Class revision */
    public static final String _REV_ID_ = "$Revision: $";

    @RequestMapping(value = "/{id}/{operation}", method = RequestMethod.POST)
    public void handleRequest(@PathVariable String id, @PathVariable String operation, HttpServletRequest request,
            HttpServletResponse response) {

        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line = reader.readLine();
            while (line != null) {
                sb.append(line + "\n");
                line = reader.readLine();
            }
            reader.close();

            JSONArray jsonObject = JSONArray.fromObject(sb.toString());
            List<Map> list = (List<Map>) JSONArray.toCollection(jsonObject, Map.class);
            for (Map map : list) {
                System.out.println(map.get("msisdn"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
